<template>
  <div class="app-container">
    <div class="custom_card_0" v-show="showSearch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="模板分类" prop="templateClassification">
          <el-select
            v-model="queryParams.templateClassification"
            placeholder="请选择模板分类"
            @change="templateClassificationChange"
          >
            <el-option label="优惠券模板" value="1"></el-option>
            <el-option label="车型模板" value="2"></el-option>
            <el-option label="节日模板" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模板类型" prop="templateType">
          <el-select
            v-model="queryParams.templateType"
            placeholder="请选择模板类型"
            @change="templateTypeChange"
          >
            <el-option
              label="优惠券图文活动"
              value="1"
              v-if="
                queryParams.templateClassification == 1 ||
                queryParams.templateClassification == 3
              "
            ></el-option>
            <el-option
              label="车型图文活动"
              value="2"
              v-if="
                queryParams.templateClassification == 2 ||
                queryParams.templateClassification == 3
              "
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模板名称" prop="templateName">
          <el-select
            v-model="queryParams.templateName"
            placeholder="请选择模板名称"
            clearable
          >
            <template
              v-for="dict in dict.type.coupon_template"
              v-if="
                queryParams.templateType == 1 &&
                queryParams.templateClassification == 1
              "
            >
              <el-option
                :label="dict.label"
                :value="dict.value"
                :key="dict.value"
              ></el-option>
            </template>
            <template
              v-for="dict in dict.type.model_template"
              v-if="
                queryParams.templateType == 2 &&
                queryParams.templateClassification == 2
              "
            >
              <el-option
                :label="dict.label"
                :value="dict.value"
                :key="dict.value"
              ></el-option>
            </template>
            <template
              v-for="dict in dict.type.holiday_coupon_template"
              v-if="
                queryParams.templateType == 1 &&
                queryParams.templateClassification == 3
              "
            >
              <el-option
                :label="dict.label"
                :value="dict.value"
                :key="dict.value"
              ></el-option>
            </template>
            <template
              v-for="dict in dict.type.holiday_model_template"
              v-if="
                queryParams.templateType == 2 &&
                queryParams.templateClassification == 3
              "
            >
              <el-option
                :label="dict.label"
                :value="dict.value"
                :key="dict.value"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:template:add']"
            >新增</el-button
          >
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="templateList" border>
        <el-table-column
          label="模板Code"
          align="center"
          prop="code"
          min-width="140"
          show-overflow-tooltip
        />
        <el-table-column
          label="模板分类"
          align="center"
          prop="templateClassification"
          min-width="140"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{
              scope.row.templateClassification == 1
                ? '优惠券模板'
                : scope.row.templateClassification == 2
                ? '车型模板'
                : '节日模板'
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="模板类型"
          align="center"
          prop="templateType"
          min-width="140"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{
              scope.row.templateType == 1 ? '优惠券图文活动' : '车型图文活动'
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="模板名称"
          align="center"
          prop="templateName"
          min-width="140"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span
              v-if="
                scope.row.templateType == 1 &&
                scope.row.templateClassification == 1
              "
              ><dict-tag
                :options="dict.type.coupon_template"
                :value="scope.row.templateName"
            /></span>
            <span
              v-if="
                scope.row.templateType == 2 &&
                scope.row.templateClassification == 2
              "
              ><dict-tag
                :options="dict.type.model_template"
                :value="scope.row.templateName"
            /></span>
            <span
              v-if="
                scope.row.templateType == 1 &&
                scope.row.templateClassification == 3
              "
              ><dict-tag
                :options="dict.type.holiday_coupon_template"
                :value="scope.row.templateName"
            /></span>
            <span
              v-if="
                scope.row.templateType == 2 &&
                scope.row.templateClassification == 3
              "
              ><dict-tag
                :options="dict.type.holiday_model_template"
                :value="scope.row.templateName"
            /></span>
          </template>
        </el-table-column>
        <el-table-column
          label="模板标题"
          align="center"
          prop="templateName"
          min-width="140"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-popover placement="bottom" width="300" trigger="click">
              <el-table :data="scope.row.headlineList" border>
                <el-table-column
                  label="模板标题"
                  align="center"
                  prop="value"
                  min-width="250"
                  show-overflow-tooltip
                >
                </el-table-column>
              </el-table>
              <el-button slot="reference" icon="el-icon-search">标题</el-button>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          label="模板内容"
          align="center"
          prop="templateName"
          min-width="140"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-popover placement="bottom" width="300" trigger="click">
              <el-table :data="scope.row.contentList" border>
                <el-table-column
                  label="模板内容"
                  align="center"
                  prop="value"
                  min-width="250"
                  show-overflow-tooltip
                >
                </el-table-column>
              </el-table>
              <el-button slot="reference" icon="el-icon-search">内容</el-button>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          label="推广文案"
          align="center"
          prop="templateName"
          min-width="140"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-popover placement="bottom" width="300" trigger="click">
              <el-table :data="scope.row.copywritingList" border>
                <el-table-column
                  label="推广文案"
                  align="center"
                  prop="value"
                  min-width="250"
                  show-overflow-tooltip
                >
                </el-table-column>
              </el-table>
              <el-button slot="reference" icon="el-icon-search">文案</el-button>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          align="center"
          prop="remark"
          min-width="140"
          show-overflow-tooltip
        />
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { listTemplate, addTemplate, getTemplate } from '@/api/system/template'

export default {
  name: 'Template',
  dicts: [
    'coupon_template',
    'model_template',
    'holiday_coupon_template',
    'holiday_model_template',
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动模板表格数据
      templateList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        templateClassification: null,
        templateType: null,
        templateName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    }
  },
  created() {
    this.getList()
  },
  activated() {
    this.getList()
  },
  methods: {
    templateClassificationChange() {
      this.queryParams.templateType = null
      this.queryParams.templateName = null
    },
    templateTypeChange() {
      this.queryParams.templateName = null
    },
    /** 查询活动模板列表 */
    getList() {
      this.loading = true
      listTemplate(this.queryParams).then((response) => {
        this.templateList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: '/system/template/add' })
    },
  },
}
</script>
